<?php
	/**
	 * 商品内容页--商品与商品属性-商品类型视图模型
	 */
	class GoodsContModel extends ViewModel{
		protected $tableName = 'goods';

		protected $viewFields = array(
			'goods' => array(
			'id', 'name', 'mprice', 'sprice', 'unit', 'inventory', 'click', 'time', 'number','cid',
			'_type' => 'LEFT'
			),
		'intro' => array(
			'mini', 'medium', 'max', 'intro', 'service',
			'_on' => 'goods.id = intro.gid',
			'_type' => 'LEFT'
			),
		'brand' => array(
			'id' => 'bid',
			'name' => 'bname',
			'logo',
			'_on' => 'goods.bid = brand.id'
			)
		);

		//获取商品详细信息
		public function getGoodsInfo($gid){
			$where = array('id' => $gid);
			$result = $this->where($where)->find();
			$result['max'] = explode('|', $result['max']);
			$result['medium'] = explode('|', $result['medium']);
			$result['mini'] = explode('|', $result['mini']);
			//实例化商品属性视图模型
			$db = D('GoodsAttr');
			$where = array('gid' => $gid);
			$attr = $db->where($where)->select();
			//对商品具有的规格和属性进行组合
			$result['attr'] = array();
			$result['spec'] = array();
			foreach($attr as $val){
				if ($val['type']) {
					$result['spec'][$val['name']][] = array(
						'id' => $val['id'],
						'value' => $val['value'],
						'type' => 'spec'
						);
					}else{
						$result['attr'][] = array(
						'name' => $val['name'],
						'value' => $val['value'],
						);
					}
			}
			return $result;
		}
	}
?>